package com.ruoyi.datastatistics.service.impl;

import java.util.*;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.datastatistics.vo.DayOrMonthDosageStatisticsBill;
import com.ruoyi.datastatistics.vo.XbStatisticAnalysisReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.datastatistics.mapper.BillDetailMapper;
import com.ruoyi.datastatistics.domain.BillDetail;
import com.ruoyi.datastatistics.service.IBillDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
/**
 * 开账明细Service业务层处理
 *
 * @author zt
 * @date 2025-05-27
 */
@Service
public class BillDetailServiceImpl extends ServiceImpl<BillDetailMapper, BillDetail> implements IBillDetailService
{
    @Autowired
    private BillDetailMapper billDetailMapper;

    /**
     * 查询开账明细
     *
     * @param id 开账明细主键
     * @return 开账明细
     */
    @Override
    public BillDetail selectBillDetailById(Long id)
    {

        return billDetailMapper.selectById(id);
    }

    /**
     * 查询开账明细列表
     *
     * @param billDetail 开账明细
     * @return 开账明细
     */
    @Override
    public List<BillDetail> selectBillDetailList(BillDetail billDetail)
    {
       //  LambdaQueryWrapper<BillDetail> queryWrapper = new LambdaQueryWrapper<>();
       //  if(!StringUtils.isEmpty(billDetail.getEnergyUseClassification())){
       //      queryWrapper.eq(BillDetail::getEnergyUseClassification,billDetail.getEnergyUseClassification());
       //  }
       //  if(!StringUtils.isEmpty(billDetail.getUserId())){
       //      queryWrapper.eq(BillDetail::getUserId,billDetail.getUserId());
       //  }
       //  if(!StringUtils.isEmpty(billDetail.getTerminalId())){
       //      queryWrapper.eq(BillDetail::getTerminalId,billDetail.getTerminalId());
       //  }
       //  if(billDetail.getStartReading() != null){
       //      queryWrapper.eq(BillDetail::getStartReading,billDetail.getStartReading());
       //  }
       //  if(billDetail.getEndReading() != null){
       //      queryWrapper.eq(BillDetail::getEndReading,billDetail.getEndReading());
       //  }
       //  if(billDetail.getTotalVal() != null){
       //      queryWrapper.eq(BillDetail::getTotalVal,billDetail.getTotalVal());
       //  }
       //  if(billDetail.getUnitPrice() != null){
       //      queryWrapper.eq(BillDetail::getUnitPrice,billDetail.getUnitPrice());
       //  }
       //  if(billDetail.getLateFee() != null){
       //      queryWrapper.eq(BillDetail::getLateFee,billDetail.getLateFee());
       //  }
       //  if(billDetail.getReadDate() != null){
       //      queryWrapper.eq(BillDetail::getReadDate,billDetail.getReadDate());
       //  }
       //  if(billDetail.getStatus() != null){
       //      queryWrapper.eq(BillDetail::getStatus,billDetail.getStatus());
       //  }
       // if(billDetail.getLocationNb() != null){
       //      queryWrapper.eq(BillDetail::getLocationNb,billDetail.getLocationNb());
       // }
        return billDetailMapper.selectList(billDetail);
    }


    @Override
    public List<DayOrMonthDosageStatisticsBill> selectBillDetailList(XbStatisticAnalysisReq req) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("sheetsCode", req.getSheetsCode());
        paramMap.put("energyUseClassification", req.getEnergyUseClassification());
        paramMap.put("beginTime", req.getBeginTime());
        paramMap.put("endTime", req.getEndTime());
        paramMap.put("userId", req.getUserId());
        paramMap.put("userName", req.getUserName());
        paramMap.put("terminalId", req.getTerminalId());
        paramMap.put("locationNb",req.getLocationNb());
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (user != null) {
            if (!Objects.equals(user.getSystemFlag(), Constants.SYS_FLAG_ADMIN)) {
                Long userId = user.getUserId();
                paramMap.put("sysUserId",userId);
            }
            if (Objects.equals(user.getSystemFlag(), Constants.SYS_FLAG_NORMAL_USER)) {
                paramMap.put("tenantId",user.getTenantId());
            }
        }
        return billDetailMapper.selectBillDetailList(paramMap);
    }

    /**
     * 新增开账明细
     *
     * @param billDetail 开账明细
     * @return 结果
     */
    @Override
    public int insertBillDetail(BillDetail billDetail)
    {
        return billDetailMapper.insert(billDetail);
    }

    /**
     * 每天生成用户日报表
     * @param freezeTime
     * @return
     */
    @Override
    public int insertBillDetailOneDayForElectricityTerminals(String freezeTime)
    {
        return billDetailMapper.insertForElectricityTerminalsOneDay(freezeTime);

    }

    /**
     * 每天生成用户日报表
     * @param freezeTime
     * @return
     */
    @Override
    public int insertBillDetailOneDayForWaterTerminals(String freezeTime)
    {
        return billDetailMapper.insertForWaterTerminalsOneDay(freezeTime);
    }
    /**
     *新增开账明细-电
     * @param frequencyType
     * @param month
     * @param freezeTime
     * @param userId
     * @return
     */
    @Override
    public int insertBillDetailForElectricityTerminals(String frequencyType, String month, String freezeTime, String userId)
    {
        if("1".equals(frequencyType)){
            //正常月报表
            return billDetailMapper.insertForElectricityTerminalsForOne(frequencyType, month, freezeTime, userId);
        }else if("2".equals(frequencyType)) {
            //月中离场
            return billDetailMapper.insertForElectricityTerminalsForTwo(frequencyType, month, freezeTime, userId);
        }else if("3".equals(frequencyType)){
            //相关用户月中报表
            return billDetailMapper.insertForElectricityTerminalsForThree(frequencyType, month, freezeTime, userId);
        }else {
            return 0;
        }

    }

    /**
     *
     * @param frequencyType
     * @param month
     * @param freezeTime
     * @param userId
     * @return
     */
    @Override
    public int insertBillDetailForWaterTerminals(String frequencyType, String month, String freezeTime, String userId)
    {
        if("1".equals(frequencyType)){
            //正常月报表
            return billDetailMapper.insertForWaterTerminalsForOne(frequencyType, month, freezeTime, userId);
        }else if("2".equals(frequencyType)) {
            //月中离场
            return billDetailMapper.insertForWaterTerminalsForTwo(frequencyType, month, freezeTime, userId);
        }else if("3".equals(frequencyType)){
            //相关用户月中报表
            return billDetailMapper.insertForWaterTerminalsForThree(frequencyType, month, freezeTime, userId);
        }else {
            return 0;
        }
    }
    /**
     * 修改开账明细
     *
     * @param billDetail 开账明细
     * @return 结果
     */
    @Override
    public int updateBillDetail(BillDetail billDetail)
    {
        billDetail.setUpdateTime(DateUtils.getNowDate());
        return billDetailMapper.updateById(billDetail);
    }

    /**
     * 批量删除开账明细
     *
     * @param ids 需要删除的开账明细主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBillDetailByIds(Long[] ids)
    {
        return billDetailMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除开账明细信息
     *
     * @param id 开账明细主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBillDetailById(Long id)
    {
        return billDetailMapper.deleteById(id);
    }
}
